#include "setwidget.h"


SetWidget::SetWidget(QWidget *parent) : QWidget(parent)
{

    initUI();
    this->model=new BaseModel();
    this->resize(600,700);
}

void SetWidget::initUI()
{
    this->timer2=new QTimer();


    this->title=new QLabel("设置界面",this);
    this->deviceNum=new QLabel("设备编号",this);
    this->memorySpace=new QLabel("内存空间",this);
    this->memoryRemain=new QLabel("内存剩余",this);
    this->videoStorage=new QLabel("录像存储",this);
    this->imageStorage=new QLabel("图片存储",this);
    this->detection=new QLabel("检测中",this);

    this->deviceNum_edit=new QComboBox(this);
    //this->deviceNum_edit->setReadOnly(true);
    this->memorySpace_edit=new QLineEdit(this);
    this->memorySpace_edit->setReadOnly(true);
    this->memoryRemain_edit=new QLineEdit(this);
    this->memoryRemain_edit->setReadOnly(true);
    this->videoStorage_edit=new QLineEdit(this);
    this->videoStorage_edit->setReadOnly(true);
    this->imageStorage_edit=new QLineEdit(this);
    this->imageStorage_edit->setReadOnly(true);

    this->videoStorage_button=new QPushButton(" ",this);
    this->imageStorage_button=new QPushButton(" ",this);


    this->ensure=new QPushButton("确认",this);
    this->cancel=new QPushButton("取消",this);

    this->layout_H1=new QHBoxLayout();
    this->layout_H2=new QHBoxLayout();
    this->layout_H3=new QHBoxLayout();
    this->layout_H4=new QHBoxLayout();
    this->layout_H5=new QHBoxLayout();
    this->layout_H6=new QHBoxLayout();
    this->titlelayout=new QHBoxLayout();
    this->layout_H7=new QHBoxLayout();


    this->layout_V1=new QVBoxLayout();


    QFont font;
    font.setPixelSize(30);
    font.setBold(true);//字体加粗

    title->setFont(font);
    deviceNum->setFont(font);
    memorySpace->setFont(font);
    memoryRemain->setFont(font);
    videoStorage->setFont(font);
    imageStorage->setFont(font);
    detection->setFont(font);

    deviceNum_edit->setFont(font);
    memorySpace_edit->setFont(font);
    memoryRemain_edit->setFont(font);
    videoStorage_edit->setFont(font);
    imageStorage_edit->setFont(font);

    videoStorage_button->setFont(font);
    imageStorage_button->setFont(font);

    ensure->setFont(font);
    cancel->setFont(font);



    layout_V1->setSpacing(50);  // 调整垂直间距为 10，可根据实际情况修改
    layout_V1->setContentsMargins(100, 100, 100, 100);  // 设置整体边距


    int horizontalSpacing = 10; // 可根据需要调整
    layout_H1->setSpacing(horizontalSpacing);
    layout_H2->setSpacing(horizontalSpacing);
    layout_H3->setSpacing(horizontalSpacing);
    layout_H4->setSpacing(horizontalSpacing);
    layout_H5->setSpacing(horizontalSpacing);
    layout_H6->setSpacing(horizontalSpacing);
    layout_H7->setSpacing(horizontalSpacing);



    // 调整图片存储相关控件的大小
        QSizePolicy imageEditPolicy = imageStorage_edit->sizePolicy();
        imageEditPolicy.setHorizontalStretch(3); // 增大输入框的水平拉伸因子
        imageStorage_edit->setSizePolicy(imageEditPolicy);
        imageStorage_edit->setMinimumWidth(200); // 设置输入框的最小宽度

        QSizePolicy imageBoxPolicy = imageStorage_button->sizePolicy();
        imageBoxPolicy.setHorizontalStretch(1); // 减小下拉框的水平拉伸因子
        imageStorage_button->setSizePolicy(imageBoxPolicy);
        imageStorage_button->setMaximumWidth(25); // 设置下拉框的最大宽度



        //录像存储相关空间的大小

        QSizePolicy videoEditPolicy = videoStorage_edit->sizePolicy();
        videoEditPolicy.setHorizontalStretch(3); // 增大输入框的水平拉伸因子
        videoStorage_edit->setSizePolicy(videoEditPolicy);
        videoStorage_edit->setMinimumWidth(200); // 设置输入框的最小宽度

        QSizePolicy videoBoxPolicy = videoStorage_button->sizePolicy();
        videoBoxPolicy.setHorizontalStretch(1); // 减小下拉框的水平拉伸因子
        videoStorage_button->setSizePolicy(videoBoxPolicy);
        videoStorage_button->setMaximumWidth(25); // 设置下拉框的最大宽度



        // 调整按钮大小和宽度
           QSize buttonSize(80, 80); // 你可以根据需要调整按钮的大小
           ensure->setMinimumSize(buttonSize);
           cancel->setMinimumSize(buttonSize);


           int buttonWidth = 100; // 可根据需求调整按钮宽度
           ensure->setFixedWidth(buttonWidth);
           cancel->setFixedWidth(buttonWidth);


           //样式表改变编辑框的形态
           QString lineEditStyle = "QLineEdit {"
                                      "  border: 2px solid #ccc;"  // 边框样式，2px 宽，灰色
                                      "  border-radius: 5px;"      // 圆角效果，半径为 5px
                                      "  padding: 5px;"            // 内边距为 5px
                                      "  background-color: #f9f9f9;" // 背景颜色为浅灰色
                                      "  color: #333;"              // 字体颜色为深灰色
                                      "}"
                                      "QLineEdit:focus {"
                                      "  border-color: #007BFF;"    // 聚焦时边框颜色变为蓝色
                                      "}";

              // 应用样式表到所有编辑框
              deviceNum_edit->setStyleSheet(lineEditStyle);
              memorySpace_edit->setStyleSheet(lineEditStyle);
              memoryRemain_edit->setStyleSheet(lineEditStyle);
              videoStorage_edit->setStyleSheet(lineEditStyle);
              imageStorage_edit->setStyleSheet(lineEditStyle);



              //样式表改变按钮
                  QString buttonStyle = "QPushButton {"
                                        "  background-color: white;"  // 正常状态背景颜色为白色
                                        "  color: black;"               // 文字颜色为黑色
                                        "  border: none;"               // 无边框
                                        "  border-radius: 5px;"         // 圆角效果，半径为 5px
                                        "  padding: 10px 20px;"         // 内边距，上下 10px，左右 20px
                                        "  font-size: 30px;"            // 字体大小为 16px
                                        "  font-weight: bold;"          // 字体加粗
                                        "}"
                                        "QPushButton:hover {"
                                        "  background-color: #0056b3;"  // 鼠标悬停时背景颜色变深
                                        "}"
                                        "QPushButton:pressed {"
                                        "  background-color: #003d80;"  // 鼠标点击时背景颜色更深
                                        "}";

                  // 应用样式表到按钮
                  ensure->setStyleSheet(buttonStyle);
                  cancel->setStyleSheet(buttonStyle);



    //整体布局

    this->titlelayout->addWidget(title);
    this->titlelayout->setAlignment(Qt::AlignCenter);  // 标题居中
    this->detection->setAlignment(Qt::AlignCenter);

    this->layout_H1->addWidget(deviceNum);
    this->layout_H1->addWidget(deviceNum_edit);
    this->layout_H2->addWidget(memorySpace);
    this->layout_H2->addWidget(memorySpace_edit);
    this->layout_H3->addWidget(memoryRemain);
    this->layout_H3->addWidget(memoryRemain_edit);
    this->layout_H4->addWidget(videoStorage);
    this->layout_H4->addWidget(videoStorage_edit);
    this->layout_H4->addWidget(videoStorage_button);
    this->layout_H5->addWidget(imageStorage);
    this->layout_H5->addWidget(imageStorage_edit);
    this->layout_H5->addWidget(imageStorage_button);
    this->layout_H7->addWidget(detection);
    this->layout_H6->addWidget(ensure);
    this->layout_H6->addWidget(cancel);



    this->layout_V1->addLayout(titlelayout);
    this->layout_V1->addLayout(layout_H1);
    this->layout_V1->addLayout(layout_H2);
    this->layout_V1->addLayout(layout_H3);
    this->layout_V1->addLayout(layout_H4);
    this->layout_V1->addLayout(layout_H5);
    this->layout_V1->addLayout(layout_H7);
    this->layout_V1->addLayout(layout_H6);

    this->setLayout(layout_V1);


//    QList<QCameraInfo>camers=QCameraInfo::availableCameras();
//    QString str=camers[0].description();
//    this->deviceNum_edit->setText(str);
//    this->devicenum=str;


                  //设备编号选择
                  QList<QCameraInfo>camers=QCameraInfo::availableCameras();
                  QList<QCameraInfo>::iterator iter=camers.begin();
                  int index=0;
                  while(iter!=camers.end())
                  {
                      QString str=camers[index++].description();
                      qDebug()<<str;
                      this->deviceNum_edit->addItem(str);
                      iter++;
                  }



    connect(videoStorage_button,&QPushButton::clicked,this,&SetWidget::fileopen_video);
    connect(imageStorage_button,&QPushButton::clicked,this,&SetWidget::fileopen_image);
    connect(ensure,&QPushButton::clicked,this,&SetWidget::ensureButton);
    connect(cancel,&QPushButton::clicked,this,&SetWidget::cancelButton);


    this->timer2->start(2000);//开始计时
    connect(this->timer2,&QTimer::timeout,this,&SetWidget::TimeOver);



}

//计算电脑内存
DiskCapacityInfo SetWidget::getDiskCapacity(const QString &path)
{






    DiskCapacityInfo info;
    QFileInfo fileInfo(path);
     //把决定路径解析扣下盘
    QString drive = fileInfo.absolutePath().section(":", 0, 0) + ":";


    ULARGE_INTEGER freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes;

        if (GetDiskFreeSpaceEx(reinterpret_cast<const wchar_t*>(drive.utf16()), &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes)) {
            info.totalCapacity = totalNumberOfBytes.QuadPart;
            info.freeSpace = freeBytesAvailable.QuadPart;
        } else {
            info.totalCapacity = -1;
            info.freeSpace = -1;
        }
        return info;
}



QString SetWidget::getDevicenum() const
{
    return devicenum;
}

void SetWidget::setDevicenum(const QString &value)
{
    devicenum = value;
}

void SetWidget::fileopen_image()
{
    QString directoryPath = QFileDialog::getExistingDirectory(nullptr, "选择文件夹", "../image", QFileDialog::ShowDirsOnly);
    QDir baseDir(QCoreApplication::applicationDirPath()); // 基准目录
    this->imagePath=baseDir.relativeFilePath(directoryPath);
    this->imageStorage_edit->setText(directoryPath);


}

void SetWidget::ensureButton()
{
    if(this->videoStorage_edit->text().size()==0||this->imageStorage_edit->text().size()==0)
    {
        QMessageBox::warning(nullptr,"警告","所选的文件夹路径不能为空");
        return;
    }


    QString sql=QString("insert into device_info (device_name, memory_size, remaining_memory, video_storage_address, image_storage_address) values('%1','%2','%3','%4','%5');").arg(deviceNum_edit->currentText(),
                                                                                         memorySpace_edit->text(),
                                                                                         memoryRemain_edit->text(),
                                                                                         this->videoPath,
                                                                                         this->imagePath
            );

    this->model->insert((char *)sql.toStdString().c_str());

    emit ensureFinished();
}

void SetWidget::cancelButton()
{
    this->videoStorage_edit->clear();
    this->imageStorage_edit->clear();
}

void SetWidget::TimeOver()
{
    QString relativePath = "../data";
    QString absolutePath = QDir::current().filePath(relativePath);

    DiskCapacityInfo info=getDiskCapacity(absolutePath);
    QString totalCapacityStr = QString::number(info.totalCapacity / (1024.0 * 1024 * 1024), 'f', 2) + " GB";
    QString freeSpaceStr = QString::number(info.freeSpace / (1024.0 * 1024 * 1024), 'f', 2) + " GB";
    this->memorySpace_edit->setText(totalCapacityStr);
    this->memoryRemain_edit->setText(freeSpaceStr);
    this->detection->setText("检测完成");
}

void SetWidget::fileopen_video()
{

    QString directoryPath = QFileDialog::getExistingDirectory(nullptr, "选择文件夹", "../video", QFileDialog::ShowDirsOnly);
    QDir baseDir(QCoreApplication::applicationDirPath()); // 基准目录
    this->videoPath=baseDir.relativeFilePath(directoryPath);
    qDebug()<<"videoPath"<<videoPath;

    this->videoStorage_edit->setText(directoryPath);
}
